package service

import (
	"go_web_template/common"
	"go_web_template/model"
)

func SaveUser(id uint, user *model.User) {

	db := common.GetDB()

	if id > 0 {
		db.Model(&model.User{}).Where("id = ?", id).Update(user)
	} else {
		//开启事务
		tx := db.Begin()
		defer func() {
			AutoTransaction(tx)
		}()
		tx = tx.Create(user)
		tx = tx.Create(&model.Balance{UserId: user.ID, Balance: 1000})
	}
}

func DelUser(id uint) {
	db := common.GetDB()
	db.Delete(&model.User{}, id)
}

func GetUser(id uint, user *model.User) {
	db := common.GetDB()
	db.Where("id = ?", id).Find(user)
}
